package com.xacero.sw;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.World.Environment;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

public class SimpleWorldAPI {
   String server_dir = (new File(".")).getAbsolutePath().substring(0, (new File(".")).getAbsolutePath().length() - 1);

   public boolean isWorldLoaded(String name) {
      World w = Bukkit.getWorld(name);
      return w != null;
   }

   public boolean doesWorldExist(String name) {
      if (this.isWorldLoaded(name)) {
         return true;
      } else {
         File world_dir = new File(this.server_dir + File.separator + name);
         File level_dat = new File(this.server_dir + File.separator + name + File.separator + "level.dat");
         return world_dir.exists() && level_dat.exists();
      }
   }

   public boolean loadWorld(String name, Environment e) {
      if (!this.doesWorldExist(name)) {
         return false;
      } else if (this.isWorldLoaded(name)) {
         return true;
      } else {
         WorldCreator wc = new WorldCreator(name);
         wc.environment(e);
         Bukkit.createWorld(wc);
         return this.isWorldLoaded(name);
      }
   }

   public boolean deleteWorld(String name) {
      if (!this.doesWorldExist(name)) {
         return false;
      } else {
         if (this.isWorldLoaded(name)) {
            this.unloadWorld(name);
         }

         File world_dir = new File(this.server_dir + File.separator + name);
         deleteFolder(world_dir);
         return !world_dir.exists();
      }
   }

   public static void deleteFolder(File folder) {
      File[] files = folder.listFiles();
      if (files != null) {
         File[] var5 = files;
         int var4 = files.length;

         for(int var3 = 0; var3 < var4; ++var3) {
            File f = var5[var3];
            if (f.isDirectory()) {
               deleteFolder(f);
            } else {
               f.delete();
            }
         }
      }

      folder.delete();
   }

   public World getWorldByName(String name) {
      if (!this.doesWorldExist(name)) {
         return null;
      } else {
         return !this.isWorldLoaded(name) ? null : Bukkit.getWorld(name);
      }
   }

   public boolean unloadWorld(String name) {
      if (!this.doesWorldExist(name)) {
         return false;
      } else if (!this.isWorldLoaded(name)) {
         return true;
      } else {
         World w = this.getWorldByName(name);
         w.save();
         Iterator var4 = w.getPlayers().iterator();

         while(var4.hasNext()) {
            Player p = (Player)var4.next();
            p.teleport(((World)Bukkit.getWorlds().get(0)).getSpawnLocation());
         }

         Bukkit.unloadWorld(name, true);
         return !this.isWorldLoaded(name);
      }
   }

   public List<World> getLoadedWorlds() {
      return Bukkit.getWorlds();
   }

   public boolean teleportEntityToWorld(Entity e, String w) {
      if (!this.doesWorldExist(w)) {
         return false;
      } else if (!this.isWorldLoaded(w)) {
         return false;
      } else {
         e.teleport(this.getWorldByName(w).getSpawnLocation());
         return e.getWorld().getName().equals(w);
      }
   }

   public boolean createWorld(String name, Environment e, boolean g) {
      if (this.doesWorldExist(name)) {
         return false;
      } else {
         WorldCreator wc = new WorldCreator(name);
         wc.environment(e);
         wc.generateStructures(g);
         Bukkit.createWorld(wc);
         return this.isWorldLoaded(name);
      }
   }

   public Environment getEnvironmentFromString(String env) {
      if (env.equalsIgnoreCase("nether")) {
         return Environment.NETHER;
      }
      if (env.equalsIgnoreCase("end")) {
         return Environment.THE_END;
      }
      return Environment.NORMAL;
   }
}
